﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Runtime.Serialization;

namespace WebPALTT.Lib
{
    /*
     * Class: HttpResponse
     * 
     * This class represents a web response for storing response information.
     * This allows the original response object to close.
     */
    [Serializable()]
    public class HttpResponse : ISerializable
    {
        /*
         * Name: Properties
         * 
         * {{/string/ Content}}:
         * 
         * Gets or sets the content of the response.
         * 
         * {{/string/ ContentEncoding}}:
         * 
         * Gets or sets the content encoding of the response.
         * 
         * {{/long/ ContentLength}}:
         * 
         * Gets or sets the content length of the response.
         * 
         * {{/string/ ContentType}}:
         * 
         * Gets or sets the content type of the response.
         * 
         * {{/CookieCollection/ Cookies}}:
         * 
         * Gets or sets the cookies of the response.
         * 
         * {{/WebHeaderCollection/ Headers}}:
         * 
         * Gets or sets the headers of the response.
         * 
         * {{/string/ Method}}:
         * 
         * Gets or sets the method of the response.
         * 
         * {{/Uri/ ResponseUri}}:
         * 
         * Gets or sets the response uri of the response.
         * 
         * {{/HttpStatusCode/ StatusCode}}:
         * 
         * Gets or sets the status code of the response.
         * 
         * {{/string/ StatusDescription}}:
         * 
         * Gets or sets the status description of the response.
         * 
         */

        public string Content;
        public string ContentEncoding;
        public long ContentLength;
        public string ContentType;
        public CookieCollection Cookies;
        public WebHeaderCollection Headers;
        public string Method;
        public Uri ResponseUri;
        public HttpStatusCode StatusCode;
        public string StatusDescription;

        public HttpResponse(HttpWebResponse response)
        {
            this.ContentEncoding = response.ContentEncoding;
            this.ContentLength = response.ContentLength;
            this.ContentType = response.ContentType;
            this.Cookies = response.Cookies;
            this.Headers = response.Headers;
            this.Method = response.Method;
            this.ResponseUri = response.ResponseUri;
            this.StatusCode = response.StatusCode;
            this.StatusDescription = response.StatusDescription;

            StreamReader streamR = new StreamReader(response.GetResponseStream());
            this.Content = streamR.ReadToEnd();
            response.Close();
        }

        public HttpResponse(SerializationInfo info, StreamingContext ctxt)
        {
            this.ContentEncoding = info.GetString("ContentEncoding");
            this.ContentLength = info.GetInt64("ContentLength");
            this.ContentType = info.GetString("ContentType");
            this.Cookies = (CookieCollection)info.GetValue("Cookies", typeof(CookieCollection));
            this.Headers = (WebHeaderCollection)info.GetValue("Headers", typeof(WebHeaderCollection));
            this.Method = info.GetString("Method");
            this.ResponseUri = (Uri)info.GetValue("ResponseUri", typeof(Uri));
            this.StatusCode = (HttpStatusCode)info.GetValue("StatusCode", typeof(HttpStatusCode));
            this.StatusDescription = info.GetString("StatusDescription");
            this.Content =  info.GetString("Content");
        }

        public void GetObjectData(SerializationInfo info, StreamingContext ctxt)
        {
            info.AddValue("ContentEncoding", ContentEncoding);
            info.AddValue("ContentLength", ContentLength);
            info.AddValue("ContentType", ContentType);
            info.AddValue("Cookies", Cookies);
            info.AddValue("Headers", Headers);
            info.AddValue("Method", Method);
            info.AddValue("ResponseUri", ResponseUri);
            info.AddValue("StatusCode", StatusCode);
            info.AddValue("StatusDescription", StatusDescription);
            info.AddValue("Content", Content);
        }
    }
}
